package cn.tedu.egweibo0416.controller;

import cn.tedu.egweibo0416.base.response.JsonResult;
import cn.tedu.egweibo0416.mapper.WeiboMapper;
import cn.tedu.egweibo0416.pojo.dto.WeiboAddParam;
import cn.tedu.egweibo0416.pojo.entity.Weibo;
import cn.tedu.egweibo0416.pojo.vo.UserVO;
import cn.tedu.egweibo0416.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo0416.pojo.vo.WeiboVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Api(tags = "02.微博模块")
@RestController
@RequestMapping("/v1/weibo/")
@Slf4j
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("content")
    public JsonResult addWeibo(@RequestBody WeiboAddParam weiboAddParam, @ApiIgnore HttpSession session){
       log.debug("weiboAddParam = " + weiboAddParam);
       UserVO userVO = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam, weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        log.debug("weibo = " + weibo);
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }

    @ApiOperation("微博首页功能")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectIndex")
    public JsonResult firstPageWeibo(){
        List<WeiboVO> list = weiboMapper.selectWeibo();
        return JsonResult.ok(list);
    }

    @ApiOperation("微博详情页功能")
    @ApiOperationSupport(order = 30)
    @GetMapping("selectByiId")
    public JsonResult selectWeiboById(@Range(min = 100, max = 500, message = "微博编号必须在100-500之间") @RequestParam Long id){
        log.debug("id = " + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
